package reit.ass3;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;


/**
 * The Class Statistics, holds statistics for simulation.
 */
class Statistics {

	/** The logger. */
	private Logger fLogger;

	/** The money gained. */
	private Long fMoneyGained;

	/** The rental requests. */
	private List<RentalRequest> fRentalRequests;

	/** The repair materials. */
	private Tradable fRepairMaterials;

	/** The repair tools. */
	private Tradable fRepairTools;

	/**
	 * Instantiates a new statistics.
	 */
	public Statistics() {
		this.fLogger = Logger.getLogger(Statistics.class);
		fMoneyGained = new Long(0);
		fRepairMaterials = new Tradable();
		fRepairTools = new Tradable();
		fRentalRequests = new ArrayList<RentalRequest>();
	}

	/**
	 * Money gained.
	 *
	 * @param money the amount of money
	 */
	public synchronized void MoneyGained(final Long money) {
		this.fMoneyGained += money;
		fLogger.info(MyStringBuilder.buildString("Collected "
				+ money.toString() + ", Total income now is"
				+ this.fMoneyGained.toString()));
	}

	/**
	 * Update repair objects.
	 *
	 * @param repairObjects the repair objects used
	 */
	public synchronized void updateRepairObjects(final List<RepairObjectInterface> repairObjects){
		fLogger.info(MyStringBuilder.buildString("updating Statstics with: "));
		List<RepairObjectInterface> repairMaterialList = new ArrayList<RepairObjectInterface>();
		List<RepairObjectInterface> repairToolList = new ArrayList<RepairObjectInterface>();

		for (RepairObjectInterface r : repairObjects ){
			if (r instanceof RepairMaterial){
				repairMaterialList.add(new RepairMaterial(r));
			}
			else if (r instanceof RepairTool){
				repairToolList.add(new RepairTool(r));
			}
			else {
				throw new RuntimeException("Statistics was given a list containing Object that is not RepairMaterial nor RepairTool");
			}
		}
		fRepairTools.checkIn(repairToolList);
		fRepairMaterials.checkIn(repairMaterialList);
		this.fLogger.info("check in complete");

	}

	/**
	 * Update rental requests.
	 *
	 * @param fRentalRequestsForStatitics the f rental requests for statitics
	 */
	public void updateRentalRequests(
			final List<RentalRequest> fRentalRequestsForStatitics) {
		this.fRentalRequests.addAll(fRentalRequestsForStatitics);

	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuilder s = new StringBuilder();
		s.append("MoneyGained: ");
		s.append(fMoneyGained);
		s.append('\n');
		s.append("Materials used: ");
		s.append(fRepairMaterials.toString());
		s.append('\n');
		s.append("Tools used: ");
		s.append(fRepairTools.toString());
		s.append('\n');
		s.append("Rental Requests status at end: ");
		s.append('\n');
		if (fRentalRequests != null) {
			for (RentalRequest r : fRentalRequests) {
				s.append(r.toString());
			}
		}
		return s.toString();
	}
	/**
	 * Update rental request.
	 *
	 * @param rentalRequest to add to statstics
	 */
	public void updateRentalRequest(RentalRequest toAdd) {
		fRentalRequests.add(toAdd);
		
	}
}
